package com.youchuang.service.impl;

import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.youchuang.bean.Book;
import com.youchuang.bean.Bulltion;
import com.youchuang.bean.Category;
import com.youchuang.bean.Comment;
import com.youchuang.bean.Product;
import com.youchuang.common.Constant;
import com.youchuang.dao.BookDAO;
import com.youchuang.dao.BulltionDAO;
import com.youchuang.dao.CategoryDAO;
import com.youchuang.dao.CategoryProductDAO;
import com.youchuang.dao.CommentDAO;
import com.youchuang.dao.ProductDAO;
import com.youchuang.service.MainService;
import com.youchuang.util.CategoryUtil;

@Transactional
@Service("mainService")
public class MainServiceImpl implements MainService {
	@Resource
	private CategoryDAO categoryDAO;
	@Resource
	private BookDAO bookDAO;
	@Resource
	private CommentDAO commentDAO;
	@Resource
	private BulltionDAO bulltionDAO;
	@Resource
	private ProductDAO productDAO;
	@Resource
	private CategoryProductDAO categoryProductDAO;

	public MainServiceImpl() {
	}

	@Override
	public List<Category> findCategoryByParentId(String parentId) {
		return categoryDAO.queryByParentId(parentId);
	}

	@Override
	public int findCategoryProductCountByCategoryId(String categoryId) {
		return categoryProductDAO.findCountByCategoryId(categoryId);
	}

	@Override
	public List<Book> findBookLimmitByCategoryId(String ctegoryId, int begin, int size) {
		return bookDAO.queryLimmitByCategoryId(ctegoryId, begin, size);
	}

	@Override
	public List<Book> findBookLimmitByCategoryIdHot(String ctegoryId, int begin, int size) {
		return bookDAO.queryLimmitByCategoryIdHot(ctegoryId, begin, size);
	}

	@Override
	public List<Book> findBookLimmitByCategoryIdNew(String ctegoryId, int begin, int size) {
		return bookDAO.queryLimmitByCategoryIdNew(ctegoryId, begin, size);
	}

	@Override
	public Book findBookById(String productId) {
		return bookDAO.queryById(productId);
	}

	@Override
	public List<Comment> findCommentByProductId(String productId) {
		return commentDAO.queryByProductId(productId);
	}

	@Override
	public List<Bulltion> findBulltionByAll() {
		return bulltionDAO.queryAll();
	}

	@Override
	public List<Category> findCategoryByAll() {
		List<Category> list = categoryDAO.queryAll();
		List<Category> cats = CategoryUtil.neatenByParentId(list, Constant.ORIGIN_CATEGORY);
		return CategoryUtil.neatenCategory(cats, list);
	}

	@Override
	public List<Product> findProductByHot() {
		return productDAO.queryLimmitByHot(0, 4);
	}

	@Override
	public List<Product> findProductByNew() {
		return productDAO.queryLimmitByNew(0, 4);
	}

	@Override
	public List<Product> findProductByNewHot(long time) {
		return productDAO.queryLimmitByHotNew(time, 0, 4);
	}

	@Override
	public Bulltion findBulltionById(String id) {
		return bulltionDAO.queryBulltionById(id);
	}

	@Override
	public List<Product> findProductLimmitByRandom() {
		int num = new Random().nextInt(20) + 1;
		return productDAO.queryLimmitByHot(num, 2);
	}

	@Override
	public List<Book> findBookLimmitByProductName(String productName) {
		String searchName = "%" + productName + "%";
		return bookDAO.queryLimmitLikeByPruductName(searchName, 0, 5);
	}

}
